﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bdxyit.com.RecruitmentAppSys.IDAL;
using bdxyit.com.RecruitmentAppSys.Models;
using System.Data.SqlClient;
using bdxyit.com.RecruitmentAppSys.DBUtility;
using System.Data;
/***
*机器名：BDXYIT-HUI
*项目名称：$projectname$
*命名空间：bdxyit.com.RecruitmentAppSys.SQLDAL
*文件名：CompanyPostModels
*系统时间：3/25/2012 3:33:33 PM
*当前用户：huihui
*创建时间：2012
*
*修改时间：2012 3/25/2012 3:33:33 PM
*修改事件：
***/

namespace bdxyit.com.RecruitmentAppSys.SQLDAL
{
    public class CompanyPostDAL : ICompanyPostDAL
    {
        private const string SQL_SELECT_COMPANYPOST = " select PostID,PostName,CompanyID,Other from CompanyPosts ";
        private const string SQL_SELECT_COMPANYPOST_BY_POSTID = " select  top 1 PostID,PostName,CompanyID,Other from CompanyPosts "
                                                             + "  where PostID=@PostID";
        private const string SQL_DELETE_COMPANYPOST_BY_POSTID = " delete from CompanyPosts "
                                                                + "  where PostID=@PostID";
        private const string SQL_UPDATE_COMPANYPOST_BY_POSTID = " update CompanyPosts set "
                                                                + " PostID=@PostID,"
                                                                + " PostName=@PostName,"
                                                                + " CompanyID=@CompanyID,"
                                                                + " Other=@Other"
                                                                + "  where PostID=@PostID";
        private const string SQL_INSERT_COMPANYPOST = " insert into CompanyPosts("
                                                    + " PostID,PostName,CompanyID,Other)"
                                                    + "  values ("
                                                    + " @PostID,@PostName,@CompanyID,@Other)";
        private const string FIELD_PostID = "@PostID";
        private const string FIELD_PostName = "@PostName";
        private const string FIELD_CompanyID = "@CompanyID";
        private const string FIELD_Other = "@Other";
        public bool InsertCompanyPost(CompanyPost cmodel)
        {
            bool flag = false;
            SqlParameter[] CompanyPostParms = GetCompanyPostParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句

            CompanyPostParms[0].Value = cmodel.PostID;
            CompanyPostParms[1].Value = cmodel.PostName;
            CompanyPostParms[2].Value = cmodel.CompanyID;
            CompanyPostParms[3].Value = cmodel.Other;
            foreach (SqlParameter parm in CompanyPostParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_INSERT_COMPANYPOST;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool DeleteCompanyPost(string PostID)
        {
            bool flag = false;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_PostID, SqlDbType.VarChar, 10);
            parm.Value = PostID;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_DELETE_COMPANYPOST_BY_POSTID;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool UpdateCompanyPost(CompanyPost cmodel)
        {
            bool flag = false;
            SqlParameter[] CompanyPostParms = GetCompanyPostParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句

            CompanyPostParms[0].Value = cmodel.PostID;
            CompanyPostParms[1].Value = cmodel.PostName;
            CompanyPostParms[2].Value = cmodel.CompanyID;
            CompanyPostParms[3].Value = cmodel.Other;
            foreach (SqlParameter parm in CompanyPostParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_UPDATE_COMPANYPOST_BY_POSTID;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public CompanyPost SelectCompanyPostByPostID(string PostID)
        {
            CompanyPost cmodel = null;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_PostID, SqlDbType.VarChar, 10);
            parm.Value = PostID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_COMPANYPOST_BY_POSTID, parm))
            {
                if (dr.Read())
                {
                    cmodel = new CompanyPost();
                    if (dr.HasRows == true)
                    {
                        if (dr["PostID"] != null && dr["PostID"].ToString() != "")
                        {
                            cmodel.PostID = dr["PostID"].ToString();
                        }
                        if (dr["PostName"] != null && dr["PostName"].ToString() != "")
                        {
                            cmodel.PostName = dr["PostName"].ToString();
                        }
                        if (dr["CompanyID"] != null && dr["CompanyID"].ToString() != "")
                        {
                            cmodel.CompanyID = dr["CompanyID"].ToString();
                        }
                        if (dr["Other"] != null && dr["Other"].ToString() != "")
                        {
                            cmodel.Other = dr["Other"].ToString();
                        }
                    }
                }
            }
            return cmodel;
        }
        public List<CompanyPost> SelectCompanyPost()
        {
            List<CompanyPost> selectlist = new List<CompanyPost>();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_COMPANYPOST))
            {
                while (dr.Read())
                {
                    CompanyPost cmodel = new CompanyPost();
                    //写添赋值语句
                    if (dr["PostID"] != null && dr["PostID"].ToString() != "")
                    {
                        cmodel.PostID = dr["PostID"].ToString();
                    }
                    if (dr["PostName"] != null && dr["PostName"].ToString() != "")
                    {
                        cmodel.PostName = dr["PostName"].ToString();
                    }
                    if (dr["CompanyID"] != null && dr["CompanyID"].ToString() != "")
                    {
                        cmodel.CompanyID = dr["CompanyID"].ToString();
                    }
                    if (dr["Other"] != null && dr["Other"].ToString() != "")
                    {
                        cmodel.Other = dr["Other"].ToString();
                    }

                    selectlist.Add(cmodel);
                }
            }
            return selectlist;
        }
        private SqlParameter[] GetCompanyPostParameters()
        {
            SqlParameter[] parms = new SqlParameter[] {
	                new SqlParameter(FIELD_PostID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_PostName, SqlDbType.VarChar,20),
					new SqlParameter(FIELD_CompanyID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_Other, SqlDbType.VarChar,100)};
            return parms;
        }

    }
}